package gaoji;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Remarks:
 * Class name :Lock
 * Date :2023/2/15 14:24
 * Author:杨俊源
 */
public class Lock {
    public static void main(String[] args) {
  //      System.out.println(Math.round(-13.5));
        Lock2 lock2 = new Lock2();
        new Thread(lock2).start();
        new Thread(lock2).start();
        new Thread(lock2).start();



    }
}

class Lock2 implements Runnable{

    int ticketnum = 10;

    //定义lock锁
    private final ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true){
            try {
                lock.lock();
                if (ticketnum > 0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(ticketnum--);
                }else {
                    break;
                }
            }finally {
                lock.unlock();
            }

        }
    }
}
